﻿package
{
    import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.display.Loader;
	
	import flash.display.Sprite;
	public class Container extends Sprite {
		
        
	// CONSTRUCTOR:
		function Container()
        {
			// LOAD SYSTEM DATA
			_data_loader = new URLLoader();
			_data_loader.addEventListener(IOErrorEvent.IO_ERROR, 		onDataError);
			_data_loader.addEventListener(Event.COMPLETE,				onDataComplete);
			_data_loader.load( new URLRequest('content/data.xml') );
		}
		
        
    //  PUBLIC
    //  PROPERTIES
        public function get data_loader()    :URLLoader { return _data_loader;
        }
        public function get content_loader() :Loader {    return _content_loader;
        }
        public function get manifest()       :XML {       return _manifest;
        }
        
		
		private         var 
	//  PROPERTIES
        _data_loader    :URLLoader,
		_content_loader	:Loader,
        _manifest       :XML;
		
		
	// METHODS
		private function onDataError(e:IOErrorEvent):void {
			trace('data ', e);
		}
		
		private function onDataComplete(e:Event):void {
			_manifest = new XML( _data_loader.data );	// Throw data into XML structure.
			
		// LOAD INITIAL CONTENT
			_content_loader = new Loader();
			_content_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, onContentLoadError );
			_content_loader.contentLoaderInfo.addEventListener( Event.COMPLETE,        onContentLoadComplete );	
			_content_loader.load(  new URLRequest( _manifest.@init )  );
		}
		
		private function onContentLoadError(e:Event):void {
			trace(e);
		}
		
		private function onContentLoadComplete(e:Event):void
        {
            trace( 'container stage :: ', stage.displayState );
			addChild( _content_loader.contentLoaderInfo.content );
		}
		
	}
}